package com.cyan.app.workflow.util;

import android.app.Activity;
import android.content.DialogInterface;

import com.cyan.app.workflow.customview.ShowCustomDialog;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by DXW on 2016/3/2.
 */
public class BaseHelper {

    /**
     * 流转字符串方法
     *
     * @param is
     * @return
     */
    public static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
//			e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
//				e.printStackTrace();
            }
        }
        return sb.toString();
    }

    /**
     * 显示dialog
     *
     * @param activity
     *            环境
     * @param strTitle
     *            标题
     * @param strText
     *            内容
     * @param icon
     *            图标
     */
    public static void showDialog(Activity activity, String strTitle,
                                  String strText, int icon) {
        if(activity == null || activity.isFinishing()){
            return;
        }

        ShowCustomDialog.Builder tDialog = new ShowCustomDialog.Builder(activity);
        //tDialog.setIcon(icon);
        tDialog.setTitle(strTitle);
        tDialog.setMessage(strText);
        tDialog.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }

        });
        tDialog.createCustomDialog().show();
    }

    /**
     * 打印信息
     *
     * @param tag
     *            标签
     * @param info
     *            信息
     */
    public static void log(String tag, String info) {
        // Log.d(tag, info);
    }

    /**
     * 获取权限
     *
     * @param permission
     *            权限
     * @param path
     *            路径
     */
    public static void chmod(String permission, String path) {
        try {
            String command = "chmod " + permission + " " + path;
            Runtime runtime = Runtime.getRuntime();
            runtime.exec(command);
        } catch (IOException e) {
//			e.printStackTrace();
        }
    }

    /**
     * 字符串转json对象
     *
     * @param str
     * @param split
     * @return
     */
    public static JSONObject string2JSON(String str, String split) {
        JSONObject json = new JSONObject();
        try {
            String[] arrStr = str.split(split);
            for (int i = 0; i < arrStr.length; i++) {
                String[] arrKeyValue = arrStr[i].split("=");
                json.put(arrKeyValue[0],
                        arrStr[i].substring(arrKeyValue[0].length() + 1));
            }
        }

        catch (Exception e) {
//			e.printStackTrace();
        }

        return json;
    }

    //把日期转为字符串
    public static String converToString(Date date)
    {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            return df.format(date);
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }
    //把字符串转为日期
    public static String converToString(String strDate)
    {
        try {
            DateFormat df = new SimpleDateFormat("MM-dd HH:mm");
            return df.format(strDate);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    //把字符串转为日期
    public static Date converToDate(String strDate) throws Exception
    {
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            return df.parse(strDate);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
